﻿using UnityEngine;
using System.Collections;

public class Inimigo1 : MonoBehaviour 
{
	enum status : int {ANDANDO, PARADO};
	int currentStatus;

	public float awakeVel;

	public float timeForStop;
		float timeForStopOriginal;
	public float timeForInvertion;
		float timeForInvertionOriginal;

	Random random;
	public int tentativas;

	public bool invertate;
	// Use this for initialization
	void Start ()
	{
		currentStatus = (int)status.ANDANDO;
		timeForStopOriginal = timeForStop;
		timeForInvertionOriginal = timeForInvertion;
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(invertate)
		{
			transform.localScale = new Vector3(-3,3,3);
		}else
		{
			transform.localScale = new Vector3(3,3,3);
		}
	
		switch(currentStatus)
		{
			case (int)status.ANDANDO:
				if(timeForStop > 0)
				{
					timeForStop -= Time.deltaTime;
					if(!invertate)
					{
						transform.Translate(awakeVel*Time.deltaTime,0,0);
					}else
					{
						transform.Translate(-awakeVel*Time.deltaTime,0,0);
					}
				}else
				{
					timeForStop = timeForStopOriginal;
					//random = new Random(5);
					tentativas = Random.Range (1,5);
					currentStatus = (int)status.PARADO;
				}

			break;
			
			case (int)status.PARADO:
			timeForInvertion -= Time.deltaTime;
				if(timeForInvertion < 0)
				{
						if(tentativas <= 0)
						{
							currentStatus = (int)status.ANDANDO;

						}else
						{
							if(invertate)
							{invertate = false;}
							else{invertate =true;}
							tentativas--;
							timeForInvertion = timeForInvertionOriginal;
							
						}
						
				}
			break;
		}
	}
	void OnTriggerExit2D(Collider2D other) 
	{
		if(other.gameObject.tag == "areaDaFase")
		{
			Destroy(gameObject);
		}
	}
}
